package juc.countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch：
 * 在完成一组正在其他线程中执行的操作之前，它允许一个或多个线程一直等待。
 * （在计数器到达0之前，其他线程各自该干嘛干嘛，到达0之后等待的线程就可以继续执行了）
 */
public class Test {

	public static void main(String[] args) throws InterruptedException {
		// 示例：模拟1个boss+4个员工召开会议的场景：老板进入会议室等待5个人全部到达会议室才会开会
		
		// 创建公用的CountDownLatch
		CountDownLatch countDownLatch = new CountDownLatch(4); // 4表示员工人数
		
		// 模拟boss和员工陆续到达会议室
		Thread thread0 = new BossThread(countDownLatch);
		Thread thread1 = new EmployeeThread("zhangsan", countDownLatch);
		Thread thread2 = new EmployeeThread("lisi", countDownLatch);
		Thread thread3 = new EmployeeThread("wangwu", countDownLatch);
		Thread thread4 = new EmployeeThread("zhaoliu", countDownLatch);
		
		thread1.start();
		
		Thread.sleep(1000);
		
		thread0.start();  // BOSS不是第1个到
		
		Thread.sleep(1000);
		
		thread2.start();
		
		Thread.sleep(1000);
		
		thread3.start();
		
		Thread.sleep(1000);
		
		thread4.start();
	}

}
